package org.douxiaoban.client.widget;

import java.util.Timer;
import java.util.TimerTask;

import org.douxiaoban.client.R;
import org.douxiaoban.client.activity.SearchResultActivity;
import org.douxiaoban.client.app.App;
import org.douxiaoban.client.app.Assistant;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

/**
 * Created by IntelliJ IDEA.
 * User: huangzhongkun
 * Date: 11-6-6
 * Time: 下午11:07
 */
public class PopupSearchBar {
    private Context _context;
    private PopupWindow _popupWindow;
    private LayoutInflater _inflater;
    private View _view;
    private String _search_type= Assistant.STATUS_BOOK;//default category

    public PopupSearchBar(Context context){
        _context = context;
    }

    public void showSearchBar(){
        _inflater = LayoutInflater.from(_context);
        _view = _inflater.inflate(R.layout.search_activity,null,true);

        final ImageButton searchDialog = (ImageButton)_view.findViewById(R.id.search_dialog_btn);
        final EditText editText = (EditText)_view.findViewById(R.id.search_edit_text);
        LinearLayout confimSearchBtn = (LinearLayout)_view.findViewById(R.id.search_confim_lay);
         final LinearLayout boundLay = (LinearLayout)
                        _view.findViewById(R.id.search_bound_lay);

        //Jump Search
        confimSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent();
                String keyword = editText.getText().toString();

                if(keyword!=null && !keyword.trim().equals("")){
                    intent.putExtra(Assistant.KEY_OF_KEYWORD, editText.getText().toString());
                    intent.putExtra(Assistant.KEY_OF_TYPE, _search_type);
                    intent.setClass(_context, SearchResultActivity.class);
                    _context.startActivity(intent);
                }
            }
        });

        searchDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                View corpusView = _inflater.inflate(R.layout.
                        corpus_selection_dialog, null, true);
                final PopupWindow corpusWindows =
                        new PopupWindow(corpusView, getScreenWidth(), 250, true);

                corpusWindows.setAnimationStyle(R.style.Animation_SelectSearchSource);
                corpusWindows.setBackgroundDrawable(new BitmapDrawable());
                corpusWindows.showAtLocation(corpusView, Gravity.TOP, 0, 30);
                corpusWindows.update();

                //选择音乐分类进行搜索
                LinearLayout musicLay = (LinearLayout)
                        corpusView.findViewById(R.id.search_music_lay);
                musicLay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                             editText.setHint(_context.getString(R.string.search_hint_music));
                        searchDialog.setImageDrawable(_context.getResources().
                                getDrawable(R.drawable.icon_music));
                        _search_type = Assistant.STATUS_MUSIC;

                        //change color theme
                        editText.setBackgroundDrawable(_context.getResources().
                                getDrawable(R.drawable.shape_search_edit_bg_music));
                        boundLay.setBackgroundColor(_context.getResources().getColor(R.color.search_bg_music));

                        corpusWindows.dismiss();
                    }
                });

                //选择图书分类进行搜索
                LinearLayout bookLay = (LinearLayout)
                        corpusView.findViewById(R.id.search_book_lay);
                bookLay.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                             editText.setHint(_context.getString(R.string.search_hint_book));
                        searchDialog.setImageDrawable(_context.getResources().
                                getDrawable(R.drawable.icon_book));
                        _search_type = Assistant.STATUS_BOOK;

                        //change color theme
                        editText.setBackgroundDrawable(_context.getResources().
                                getDrawable(R.drawable.shape_search_edit_bg_book));
                        boundLay.setBackgroundColor(_context.getResources().getColor(R.color.search_bg_book));

                        corpusWindows.dismiss();
                    }
                });

                //选择电影分类进行搜索
                LinearLayout movieLay = (LinearLayout)
                        corpusView.findViewById(R.id.search_movie_lay);
                movieLay.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View view) {
                             editText.setHint(_context.getString(R.string.search_hint_movie));
                        searchDialog.setImageDrawable(_context.getResources().
                                getDrawable(R.drawable.icon_movice));
                        _search_type = Assistant.STATUS_MOVICE;
                        //change color theme
                        editText.setBackgroundDrawable(_context.getResources().
                                getDrawable(R.drawable.shape_search_edit_bg_movice));
                        boundLay.setBackgroundColor(_context.getResources().getColor(R.color.search_bg_movie));

                        corpusWindows.dismiss();
                    }
                });

            }
        });


        _popupWindow = new PopupWindow(_view,getScreenWidth(), App.dip2px(_context,85),true);
        _popupWindow.setAnimationStyle(R.style.Animation_SelectBar);
        _popupWindow.setBackgroundDrawable(new BitmapDrawable());
        _popupWindow.showAtLocation(_view, Gravity.TOP,0,0);
        _popupWindow.update();

        //间隔半秒，等待view加载完成之后快速弹出键盘。
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                InputMethodManager m = (InputMethodManager)editText.getContext().
                getSystemService(Context.INPUT_METHOD_SERVICE);
                m.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
            }
        },100);

    }

    public int getScreenWidth(){
            DisplayMetrics dm = new DisplayMetrics();
            dm = _context.getResources().getDisplayMetrics();

            return dm.widthPixels;
        }

}
